package com.cipe.cmrs.controllers.reports;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;

import com.cipe.cmrs.bean.SignatureReportBean;
import com.cipe.cmrs.controllers.BaseController;

import com.cipe.cmrs.model.SignatureSelectedLog;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.service.SignatureReportService;
import com.cipe.cmrs.util.UserUtil;

@Controller
public class SignatureReportController  extends BaseController {


	/**
	 * 
	 */
	private static final long serialVersionUID = -7781237859319480278L;

	@Autowired 
	private SignatureReportService signatureReportService;
	
	List<SignatureReportBean> signatureReportBeanList;
	List<SignatureSelectedLog> signatureSelectedLogList;
	SignatureSelectedLog signatureSelectedLog;
	SignatureReportBean signatureReportBean;
	
	/**
	 * This method is called when NeverSelected or Unpaid button in process payment page  is clicked .It
	 * set attributes signaturereportBeanList and signatureReportBean in the model and return jsp page named SignatureReportForm which
	 * is displayed as an popup in process Payment Request page.
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/secure/neverSelectedSignature.do")
	public String neverSelectedSignature(Model model,  HttpServletRequest request) {	
		 User user = UserUtil.INSTANCE.getCurrentUser();
		signatureReportBeanList = signatureReportService.neverSelectedSignatures(request.getParameter("selectionType"));
		signatureReportBean= new SignatureReportBean();
	    signatureReportBean.setSelectionType(request.getParameter("selectionType"));
	    signatureReportBean.setUserId(user.getId().toString());
		model.addAttribute("signatureReportBeanList", signatureReportBeanList);
		model.addAttribute("signatureReportBean",signatureReportBean);
		return "reportForms/signatureReportForm";
	}
	/**
	 * Saves all the selected signature of SignatureReportForm 
	 * @param bean
	 * @param result
	 * @param request
	 * @param status
	 */
	@RequestMapping("/secure/saveNeverSelectedSignature.do")
	public void  saveNeverSelectedSignature(@ModelAttribute("signatureReportBean") SignatureReportBean bean, BindingResult result,
	        HttpServletRequest request, SessionStatus status) {	
	
			
			String[] paymentReqId=bean.getConcatePaymentReqId().split("[|]");
	
			for(String tempPaymentReq:paymentReqId)
			{
				  
				 try 
				 {
					signatureSelectedLog=new SignatureSelectedLog();					
				    signatureSelectedLog.setPaymentReqId(Integer.parseInt(tempPaymentReq));
				    signatureSelectedLog.setCreationDate(new Date());
				    signatureSelectedLog.setKey(1);
				    signatureSelectedLog.setSelectionType(bean.getSelectionType());
				    signatureSelectedLog.setSignatureRequestFor(bean.getSignatureRequestFor());
				    signatureReportService.saveSignatureSelectedLog(signatureSelectedLog);
				    
				    
				 }catch(Exception e)
				 {
					 System.out.println("Exception caught due to internal error"+e);
				 }
				   
			}
		
		      
	}
	
	

}
